﻿using Himp.Base;
using Himp.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Erp
{
    /// <summary>
    /// 入库单
    /// </summary>
    public class InboundRecService : ErpAppService, IInboundRecService
    {
        private IRepository<InboundRec, Guid> _inboundRecRepository;
        private IBizSeqService _bizSeqService;
        private readonly IRepository<InboundDetl, Guid> _inboundDetlRepository;
        private readonly IRepository<MatlInv, Guid> _matlInvRepository;
        private InvManager _invManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public InboundRecService(IRepository<InboundRec, Guid> inboundRecRepository
            , IRepository<InboundDetl, Guid> inboundDetlRepository
            , IBizSeqService bizSeqService
            , InvManager invManager
            , IRepository<MatlInv, Guid> matlInvRepository)
        {
            _inboundDetlRepository = inboundDetlRepository;
            _inboundRecRepository = inboundRecRepository;
            _bizSeqService = bizSeqService;
            _invManager = invManager;
            _matlInvRepository = matlInvRepository;
        }

        /// <summary>
        /// 获取入库单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<InboundRecDto>> GetInboundRecListAsync(InboundRecInput input)
        {
            var iQuery = await _inboundRecRepository.WithDetailsAsync();
            iQuery = iQuery.Where(i => i.MatlWhseId == input.matlWhseId);
            iQuery = iQuery.WhereIf(!string.IsNullOrWhiteSpace(input.Filter), i => i.OrdNo.Contains(input.Filter));
            iQuery = iQuery.WhereIf(input.Begndata != null, i => i.InboundTime.Date >= input.Begndata.Value.Date);
            iQuery = iQuery.WhereIf(input.Enddata != null, i => i.InboundTime.Date <= input.Enddata.Value.Date);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<InboundRec>, List<InboundRecDto>>(iQuery.ToList());

                return new PagedResultDto<InboundRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<InboundRec>, List<InboundRecDto>>(items.ToList());

                return new PagedResultDto<InboundRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 获取入库单明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<InboundRecDto> GetInboundRecAsync(Guid id)
        {
            var inboundRec = await _inboundRecRepository.GetAsync(id);

            return ObjectMapper.Map<InboundRec, InboundRecDto>(inboundRec);
        }

        /// <summary>
        /// 创建入库单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<InboundRecDto> PostInboundRecAsync(InboundRecCreateOrUpdateDto input)
        {
            var inboundRec = ObjectMapper.Map<InboundRecCreateOrUpdateDto, InboundRec>(input);
            inboundRec.SetOrdNo(await _bizSeqService.GetAsync("入库单号"));
            inboundRec = await _inboundRecRepository.InsertAsync(inboundRec);
            var itemDto = ObjectMapper.Map<InboundRec, InboundRecDto>(inboundRec);

            return itemDto;
        }

        /// <summary>
        /// 修改入库单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<InboundRecDto> PutInboundRecAsync(Guid id, InboundRecCreateOrUpdateDto input)
        {
            var inboundRec = await _inboundRecRepository.GetAsync(id);
            inboundRec = ObjectMapper.Map(input, inboundRec);
            inboundRec = await _inboundRecRepository.InsertAsync(inboundRec);
            var itemDto = ObjectMapper.Map<InboundRec, InboundRecDto>(inboundRec);

            return itemDto;
        }

        /// <summary>
        /// 审核入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task ChkInboundRecAsync(ChkInboundRecDto input)
        {
            var matlInvs = await _invManager.ChkInboundRec(input.Id);
            await _matlInvRepository.InsertManyAsync(matlInvs);
        }

        /// <summary>
        /// 作废入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteInboundRecAsync(Guid id)
        {
            var inboundRec = await _inboundRecRepository.GetAsync(id);

            if (inboundRec.InboundStas == EnumInboundStas.未审核) 
            {
                inboundRec.InboundStas = EnumInboundStas.已作废;
            }
        }

        /// <summary>
        /// 获取入库单明细列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<InboundDetlDto>> GetInboundDetlListAsync(InboundDetlInput input)
        {
            var iQuery = await _inboundDetlRepository.WithDetailsAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<InboundDetl>, List<InboundDetlDto>>(iQuery.ToList());

                return new PagedResultDto<InboundDetlDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<InboundDetl>, List<InboundDetlDto>>(items.ToList());

                return new PagedResultDto<InboundDetlDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 创建入库单明细信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<InboundDetlDto> PostInboundDetlAsync(InboundDetlCreateOrUpdateDto input)
        {
            var inboundDetl = ObjectMapper.Map<InboundDetlCreateOrUpdateDto, InboundDetl>(input);

            if (string.IsNullOrEmpty(input.Bchno))
            {
                inboundDetl.Bchno = await _bizSeqService.GetAsync("入库批次号");
            }

            inboundDetl = await _inboundDetlRepository.InsertAsync(inboundDetl);
            var itemDto = ObjectMapper.Map<InboundDetl, InboundDetlDto>(inboundDetl);

            return itemDto;
        }
    }
}
